using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class BackPackUI : MonoBehaviour
{
    public Button rightButton, leftButton;

    public SlotUI slotUI;

    public ItemDataList_SO itemData;
    //当前物品序号
    public int currentIndex;

    private void Awake()
    {
        currentIndex = -1;
        OnUpdateUIEvent(null, currentIndex);
    }
    private void OnEnable()
    {
        EventHandler.UpdateUIEvent += OnUpdateUIEvent;
    }

    private void OnDisable()
    {
        EventHandler.UpdateUIEvent -= OnUpdateUIEvent;
    }
    private void OnUpdateUIEvent(ItemDetails item,int index)
    {
        Debug.Log(index);
        Debug.Log(item);
        if(item == null && index == -1)
        {
            slotUI.SetEmpty();
            currentIndex = -1;
            leftButton.interactable = false;
            rightButton.interactable = false; 
        }
        else
        {
            currentIndex = index;
            slotUI.SetItem(item);
            leftButton.interactable = true;
            rightButton.interactable = true;
        }
    }

    public void LeftButtonClick()
    {
        if (currentIndex == -1 && InventoryManager.GetItem().Count == 0) return;
        if(currentIndex - 1 < 0)
        {
            currentIndex = InventoryManager.GetItem().Count - 1;
            UpdateUI();
        }
        else
        {
            currentIndex--;
            UpdateUI();
        }
    }

    public void RightButtonClick()
    {
        if (currentIndex == -1) return;
        currentIndex = (currentIndex + 1) % InventoryManager.GetItem().Count;
        UpdateUI();
    }

    private void UpdateUI()
    {
        EventHandler.CallUpdateUIEvent(itemData.GetItemDetails(InventoryManager.GetItem()[currentIndex]), currentIndex);
    }
}
